﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_OtherList_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            getNgonngu()

        End If
    End Sub
    Protected Sub getNgonngu()
        ddlLanguageId.Items.Clear()
        ddlLanguageId.AppendDataBoundItems = True
        ddlLanguageId.Items.Add(New ListItem("---Chọn---", ""))
        Using data As New VSEntities
            Dim p = (From q In data.Languages Select q.LanguageId, q.Name).ToList
            ddlLanguageId.DataValueField = "LanguageId"
            ddlLanguageId.DataTextField = "Name"
            ddlLanguageId.DataSource = p
            ddlLanguageId.DataBind()
            ddlLanguageId.SelectedValue = 0
        End Using
    End Sub
    Protected Function Save() As Boolean
        Using data As New VSEntities
            Dim p As New VSModel.Otherlist
            Try
                Dim _max As String = (From q In data.OtherLists Order By q.Id Descending Where q.Type = ddlDanhMucType.SelectedValue Select q.Id).FirstOrDefault
                Dim _id As String = (CInt(_max) + 1).ToString("D3")
                p.Type = ddlDanhMucType.SelectedValue
                p.Id = _id
                p.Name = txtPhanloai.Text.Trim()
                p.PhanLoai = txtPhanloai.Text.Trim()
                'p.HinhDo = txtHinhdo.Text.Trim()
                p.TuCanhBao = txtTucanhbao.Text.Trim()
                p.CanhBaoNguyCo = txtCanhbaonguyco.Text.Trim()
                data.OtherLists.AddObject(p)
                data.SaveChanges()
                'Insert_App_Log("Insert  Otherlist:" & txtTitle.Text.Trim & "", Function_Name.Otherlist, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))

                'Inser OtherListHinhDo

                Dim arrKeyWord As String() = txtKeyword.Text.TrimEnd(";").Split(";")

                For i As Integer = 0 To arrKeyWord.Length - 1
                    If arrKeyWord(i) <> "" Then
                        Dim q As New OtherListHinhDo
                        q.OtherListType = p.Type
                        q.OtherListId = p.Id
                        q.HinhDoId = arrKeyWord(i)
                        'q.ContentId = iContentID
                        'q.KeywordId = arrKeyWord(i)
                        data.OtherListHinhDoes.AddObject(q)

                        data.SaveChanges()
                    End If
                Next


                Return True
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Return False
            End Try
        End Using
    End Function
    Protected Sub ResetControl()
        ddlDanhMucType.SelectedIndex = -1

        ddlLanguageId.SelectedIndex = -1

        txtPhanloai.Text = ""

        txtTucanhbao.Text = ""
        txtCanhbaonguyco.Text = ""
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Save() Then
            Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/OtherList/List.aspx';", Me.Page, True)
        Else
            Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnSaveAndNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAndNew.Click
        If Save() Then
            Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');", Me.Page, True)
            ResetControl()
        Else
            Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ResetControl()
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region

    Protected Sub ddlLanguageId_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlLanguageId.SelectedIndexChanged
        Using data As New VSEntities
            Dim p = (From q In data.OtherListTables Where q.LanguageId = ddlLanguageId.SelectedValue Select q.OLCode, q.OtherListName).ToList
            ddlDanhMucType.DataValueField = "OLCode"
            ddlDanhMucType.DataTextField = "OtherListName"
            ddlDanhMucType.DataSource = p
            ddlDanhMucType.DataBind()
            hplSelKeyWord.CssClass = "thickbox"
            hplSelKeyWord.NavigateUrl = "~/Page/HinhDo/HinhDo.aspx?LanguageId=" & ddlLanguageId.SelectedValue & "&keepThis=true&TB_iframe=true&height=450&width=500&modal=true"
        End Using
    End Sub


End Class
